package com.syscom.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syscom.common.base.BasePage;
import com.syscom.common.entity.SysRole;
import com.syscom.common.enums.UniversalStatus;
import com.syscom.common.mapper.SysRoleMapper;
import com.syscom.common.utils.result.AjaxResult;
import com.syscom.common.utils.result.ResultTool;
import com.syscom.core.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author wjz
 * @date 2021-01-01
 */
@Slf4j
@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SecurityUtils securityUtils;

    public AjaxResult querySysRole(BasePage basePage, String name) {

        Page<SysRole> page = new Page<>(basePage.getPageNo(), basePage.getPageSize());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(name), "name", name);
        queryWrapper.eq("deleted", UniversalStatus.NO);
        queryWrapper.orderByAsc("object_id");
        return ResultTool.success(sysRoleMapper.selectPage(page, queryWrapper));
    }

    public AjaxResult addSysRole(SysRole sysRole) {

        sysRole.setCreatedBy(securityUtils.getCurrentUserId());
        sysRole.setCreatedDate(new Date());
        sysRole.setDeleted(UniversalStatus.NO);
        return ResultTool.success(sysRoleMapper.insert(sysRole));
    }

    public AjaxResult updateSysRole(SysRole sysRole) {

        sysRole.setLastUpdateBy(securityUtils.getCurrentUserId());
        sysRole.setCreatedDate(new Date());
        return ResultTool.success(sysRoleMapper.updateById(sysRole));
    }

    public AjaxResult deleteSysRole(Long objectId) {

        SysRole sysRole = new SysRole().setDeleted(UniversalStatus.YES);
        sysRole.setObjectId(objectId);
        return ResultTool.success(sysRoleMapper.updateById(sysRole));
    }
}

